<?php
/**
 * Created by PhpStorm.
 * User: 小蛮哼哼哼
 * Email: 243194993@qq.com
 * Date: 2021/10/22
 * Time: 16:01
 * motto: 现在的努力是为了小时候吹过的牛逼！
 */

namespace app\common\business;


use app\common\lib\Show;
use app\common\lib\Status;
use app\common\validate\SeatDetail as ValObj;
use app\common\model\mysql\SeatDetail as ModelObj;
use think\Exception;
use think\facade\Log;

class SeatDetail extends Base
{
    /**
     * @var ValObj
     */
    protected ValObj $valObj;
    /**
     * @var ModelObj
     */
    protected ModelObj $modelObj;

    /**
     *
     */
    public function __construct()
    {
        $this->valObj = new ValObj();
        $this->modelObj = new ModelObj();
    }

    /**
     * @param array $data
     * @param int $seat_id
     * @return \think\Collection
     * @throws Exception
     */
    public function addSeatDetail(array $data = [], int $seat_id = 0)
    {
        //拆分数组
        $list_data = [];
        foreach ($data['all'] as $k => $v) {
            $list_data[$k] = [
                "seat_id" => $seat_id,
                "seatX" => $v[0],
                "seatY" => $v[1],
                "money" => $v[2],
                "is_disabled" => $v[3],
                "state" => $v[4]
            ];
        }
        if (empty($list_data)) {
            throw new Exception("内部错误");
        }

        $insert_data = [];
        $update_data = [];
        //查询是否有
        $this->modelObj->startTrans();
        try {
            //先更新所有的状态不正常，
            $this->modelObj->where('seat_id', $seat_id)
                ->update([
                    "status" => Status::mysql_delete,
                    "update_time" => time()
                ]);
            foreach ($list_data as $k => $v) {
                //查询是否存在
                $res[$k] = $this->modelObj->getSeatDetailBySeatIdAndSeat($v['seat_id'], $v['seatX'], $v['seatY']);
                if ($res[$k]) {
                    $update_data[$k] = $v;
                    $update_data[$k]['id'] = $res[$k]['id'];
                    $update_data[$k]['state'] = $v['state'];
                    $update_data[$k]['status'] = Status::mysql_normal;
                } else {
                    $insert_data[$k] = $v;
                }
            }
            $data_all = array_merge($insert_data, $update_data);
            $ret = $this->modelObj->saveAll($data_all);
            $this->modelObj->commit();
        } catch (\Exception $e) {
            Log::error($e->getMessage() . " " . $e->getFile() . " " . $e->getLine());
            $this->modelObj->rollback();
            throw new Exception("操作错误");
        }
        return $ret;
    }

    public function getSeatDetailByList(int $seat_id = 0, string $field = "*")
    {
        try {
            $lists = $this->modelObj->getSeatDetailByList($seat_id, $field);
        } catch (\Exception $e) {
            throw new Exception("查询失败");
        }
        return $lists;
    }

    public function getSeatDetailByIndex(int $seat_id = 0)
    {
        try {
            $seat = $this->modelObj->getSeatDetailBySeatId($seat_id);
        } catch (\Exception $e) {
            throw new Exception("查询失败");
        }
        if (!$seat) {
            throw new Exception("座位不存在");
        }
        return $seat;
        dump($seat);
        exit();
        $lists = [];
        foreach ($seat as $k => $v) {
            $lists[$k] = [
                "XCoord" => $v['seatX'],
                "YCoord" => $v['seatY'],
                "SeatCode" => $v['id'],
                "Status" => $v['is_disabled'] == 1 ? 2 : 0,
                "RowNum" => $v['seatY'],
                "ColumnNum" => $v['seatX'],
                "money" => $v['money']
            ];
        }
        return $lists;
    }

    public function getSeatDetailByIds(array $ids = [], string $field = '*')
    {
        try {
            $lists = $this->modelObj->getSeatDetailByIds($ids, $field);
        } catch (\Exception $e) {
            throw new Exception("查询失败");
        }
        return $lists;
    }

    /**
     * @param array $ids
     * @return bool
     * @throws Exception
     */
    public function deleteByDataSeat(array $ids = [])
    {
        try {
            $result = $this->modelObj->where('id', 'in', $ids)
                ->delete();
        } catch (\Exception $e) {
            throw new Exception("操作失败");
        }
        return $result;
    }

    /**
     * @param array $data
     * @param int $seat_id
     * @return bool
     * @throws Exception
     */
    public function setSeatDetailByInfo(array $data = [], int $seat_id = 0): bool
    {
        $this->modelObj->startTrans();
        try {
            foreach ($data as $value) {
                $this->modelObj
                    ->where('seat_id', $seat_id)
                    ->where('Status', '<>', -1)
                    ->where('RowNum', $value['RowNum'])
                    ->where('ColumnNum', $value['ColumnNum'])
                    ->update($value);
            }
            $this->modelObj->commit();
        } catch (\Exception $e) {
            $this->modelObj->rollback();
            throw new Exception("操作失败");
        }
        return true;
    }

    /**
     * @param int $seat_id
     * @return mixed
     * @throws Exception
     */
    public function getSeatDetailByMoneyList(int $seat_id = 0)
    {
        try {
            $money_list = $this->modelObj->field('money')
                ->where('seat_id', $seat_id)
                ->group('money')
                ->select()->toArray();
        } catch (\Exception $e) {
            throw new Exception("查询失败");
        }
        return $money_list;
    }

    /**
     * @param int $seat_id
     * @return array
     * @throws Exception
     */
    public function getSeatDetailByListStatusNoraml(int $seat_id = 0)
    {
        try {
            $lists = $this->modelObj
                ->where('seat_id', $seat_id)
                ->where('Status', 0)
                ->select()
                ->toArray();
        } catch (\Exception $e) {
            throw new Exception("查询失败");
        }
        return $lists;
    }
}
